using Digitalmes.Application.Scada.Models;
using Digitalmes.Application.Scada.Services;

namespace Digitalmes.Application.Scada.Commands;

/// <summary>
/// 进站请求命令处理者。
/// </summary>
internal sealed class InboundCommandHandler(IServiceScopeFactory serviceScopeFactory) : IRequestHandler<InboundCommand, ScadaResult>
{
    public async Task<ScadaResult> Handle(InboundCommand request, CancellationToken cancellationToken)
    {
        using var scope = serviceScopeFactory.CreateScope();
        var service = scope.ServiceProvider.GetRequiredService<IInboundService>();
        InboundInput input = new(request.LineCode, request.WsCode, request.SN, request.ProductCode, request.Operator, request.Shift, request.Rfid);
        var result = await service.EntryAsync(input, cancellationToken).ConfigureAwait(false);

        // 发布产品存档后处理事件（不等待）
        //await sender.Publish(new AfterInboundEvent(), cancellationToken).ConfigureAwait(false);

        return result;
    }
}
